<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<!--{include file='comm/header.tpl'}-->
<!--{include file='comm/comm_cal.tpl'}-->

<style>.flexigrid{margin:10px;}</style>
<title>订单促销添加</title>
<!--{include file="comm/icheck.tpl"}-->
<!--{include file='comm/comm_pic.tpl'}--> 
</head>
<body>
	<div style="width:100%;">
		
		<div class="container">

			<div class="comm_content">

                <div class="navigation" id="navigation" style="margin-bottom: 13px;">
                    <ul class="nav nav-tabs">
                        <li class="active" onclick="promotion.change(1);" id="select_1" ><a href="javascript:void(0)">订单立减</a></li>
                        <li  onclick="promotion.change(2);" id="select_2"><a href="javascript:void(0)" >免运费</a></li>
                        <li  onclick="promotion.change(3);" id="select_3"><a href="javascript:void(0)" >优惠券</a></li>
                        <li  onclick="promotion.change(4);" id="select_4"><a href="javascript:void(0)" >折扣</a></li>
                    </ul>
                </div>

                <div>
                    <form id="sendForm" class="form-horizontal" name="sendForm" onsubmit="return false;">
                        <div class="commAttr">
                            <div class="form-group">
                                <label for="title" class="col-sm-2 control-label"><span class="red">*</span> 促销标题：</label>
                                <div class="col-sm-10">
                                    <input type="text" class="form-control w300" id="title" name="title" placeholder="请输入促销标题【必填项】">
                                </div>
                                <input type="hidden" id="type" name="type" value="">
                                <input type="hidden" id="promotionID" name="promotionID" value="">
                            </div>

                            <div class="form-group">
                                <label for="billAmount" class="col-sm-2 control-label"><span class="red">*</span> 下限金额：</label>
                                <div class="col-sm-10">
                                    <input type="text" class="form-control w300" id="billAmount" name="billAmount" placeholder="请输入下限金额【必填项】">
                                </div>
                            </div>

                            <div class="form-group">
                                <div>
                                    <label class="col-sm-2 control-label"><span class="red">*</span> 促销时间：</label>
                                    <div class="col-sm-3">
                                        <input type="text" class="form-control" id="startTime" name="startTime" readonly="">
                                    </div>
                                    <div class="fl">
                                        <label class="col-sm-1 control-label">～</label>
                                    </div>
                                    <div class="col-sm-3">
                                        <input type="text" class="form-control" id="endTime" name="endTime" readonly="">
                                    </div>
                                </div>
                            </div>

                            <div class="form-group">
                                <div>
                                    <label class="col-sm-2 control-label"> 配送时间：</label>
                                    <div class="col-sm-2" style="width:130px;">
                                        <input type="text" class="form-control w100" id="sendStartTime" name="sendStartTime" readonly="">
                                    </div>
                                    <div class="fl">
                                        <label class="col-sm-1 control-label">～</label>
                                    </div>
                                    <div class="col-sm-2">
                                        <input type="text" class="form-control w100" id="sendEndTime" name="sendEndTime" readonly="">
                                    </div>
                                </div>
                            </div>

                            <div class="form-group" style="display:none;">
                                <label for="priority" class="col-sm-2 control-label"><span class="red">*</span> 优先级：</label>
                                <div class="col-sm-10">
                                    <input type="text" class="form-control w300" id="priority" name="priority" placeholder="请输入促销优先级【必填项】" value="<!--{$promotionBillInfo->sortOrder}-->">
                                </div>
                            </div>

                            <div class="form-group">
                                <label  class="col-sm-2 control-label"><span class="red">*</span> 是否使用代金卡：</label>
                                <div class="col-sm-10">
                                    <div class="fl" style="margin-right: 10px;">
                                        <input type="radio" class="square-radio" id="card-radio-2" name="use_card" value="2" checked>
                                        <label for="card-radio-2">不限</label>
                                    </div>
                                    <div class="fl" style="margin-right: 10px;">
                                        <input type="radio" class="square-radio" id="card-radio-1" name="use_card" value="1">
                                        <label for="card-radio-1">是</label>
                                    </div>
                                    <div class="fl">
                                        <input type="radio" class="square-radio" id="card-radio-0" name="use_card" value="0">
                                        <label for="card-radio-0">否</label>
                                    </div>
                                </div>
                            </div>


                            <div class="form-group" id="setUseCard" style="display: none">
                                <label class="col-sm-2 control-label">
                                    <span class="red">*</span>
                                    代金卡面值：
                                </label>
                                <div class="col-sm-10">
                                    <!--{foreach from=$card_array item=card}-->
                                    <div class="fl" style="margin: 0px 10px 10px 0px;">
                                        <input type="checkbox" class="square-radio" id="card-checkbox-<!--{$card.face_value}-->" name="use_card_face_value" value="<!--{$card.face_value}-->" <!--{if isset($promotionInfo->use_card_face_value) && $card.face_value|in_array:$promotionInfo->use_card_face_value}--> checked="checked" <!--{/if}-->>
                                        <label for="card-checkbox-<!--{$card.face_value}-->"><!--{$card.face_value}--></label>
                                    </div>
                                    <!--{/foreach}-->
                                </div>
                            </div>




                            <div class="form-group">
                                <label  class="col-sm-2 control-label"><span class="red">*</span> 是否使用优惠券：</label>
                                <div class="col-sm-10">
                                    <div class="fl" style="margin-right: 10px;">
                                        <input type="radio" class="square-radio" id="coupon-radio-2" name="use_coupon" value="2" checked>
                                        <label for="coupon-radio-2">不限</label>
                                    </div>
                                    <div class="fl" style="margin-right: 10px;">
                                        <input type="radio" class="square-radio" id="coupon-radio-1" name="use_coupon" value="1">
                                        <label for="coupon-radio-1">是</label>
                                    </div>
                                    <div class="fl">
                                        <input type="radio" class="square-radio" id="coupon-radio-0" name="use_coupon" value="0">
                                        <label for="coupon-radio-0">否</label>
                                    </div>
                                </div>
                            </div>

                            <div class="form-group" style="display:none">
                                <label  class="col-sm-2 control-label"> 是否排斥：</label>
                                <div class="col-sm-10">
                                    <div class="fl" style="margin-right: 10px;">
                                        <input type="radio" class="square-radio" id="repel-radio-1" name="repel" value="1" checked>
                                        <label for="repel-radio-1">是</label>
                                    </div>
                                    <div class="fl">
                                        <input type="radio" class="square-radio" id="repel-radio-0" name="repel" value="0">
                                        <label for="repel-radio-0">否</label>
                                    </div>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-sm-2 control-label"> 使用状态：</label>
                                <div class="col-sm-10">
                                    <div class="fl" style="margin-right: 10px;">
                                        <input type="radio" class="square-radio" id="square-radio-1" name="useFlg" value="1" checked>
                                        <label for="square-radio-1">启用</label>
                                    </div>
                                    <div class="fl">
                                        <input type="radio" class="square-radio" id="square-radio-0" name="useFlg" value="0">
                                        <label for="square-radio-0">禁用</label>
                                    </div>
                                </div>
                            </div>

                            <div class="form-group">
                                <label for="description" class="col-sm-2 control-label"> 促销描述：</label>
                                <div class="col-sm-8">
                                    <textarea  class="form-control" id="description" name="description" placeholder="请输入促销描述" ></textarea>
                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">会员等级：</label>
                            <div class="col-sm-10">
                                <!--{foreach from=$cust_rank_array item=cust}-->
                                <div class="fl" style="margin: 0px 10px 10px 0px;">
                                    <input type="checkbox" class="square-radio" id="rank-checkbox-<!--{$cust.rankID}-->" name="rankID" value="<!--{$cust.rankID}-->">
                                    <label for="rank-checkbox-<!--{$cust.rankID}-->"><!--{$cust.rankName}--></label>
                                </div>
                                <!--{/foreach}-->
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">支付方式：</label>
                            <div class="col-sm-10">
                                <!--{foreach from=$pay_type_array item=payType}-->
                                <div class="fl" style="margin: 0px 10px 10px 0px;">
                                    <input type="checkbox" class="square-radio" id="payType-checkbox-<!--{$payType.payTypeID}-->" name="payTypeID" value="<!--{$payType.payTypeID}-->">
                                    <label for="payType-checkbox-<!--{$payType.payTypeID}-->"><!--{$payType.payTypeName}--></label>
                                </div>
                                <!--{/foreach}-->
                            </div>
                        </div>

                        <!--付款方式-->
                        <div class="form-group" id="payment" style="display: none;">

                        </div>
                        <!--付款方式-->

                        <div class="form-group">
                            <label class="col-sm-2 control-label">促销渠道：</label>
                            <div class="col-sm-10">
                                <!--{foreach from=$promotion_channel_array item=promotionChannel}-->
                                <div class="fl" style="margin: 0px 10px 10px 0px;">
                                    <input type="checkbox" class="square-radio" id="promotionChannel-checkbox-<!--{$promotionChannel.itemID}-->" name="promotionChannelID" value="<!--{$promotionChannel.itemID}-->">
                                    <label for="promotionChannel-checkbox-<!--{$promotionChannel.itemID}-->"><!--{$promotionChannel.itemName}--></label>
                                </div>
                                <!--{/foreach}-->
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">城市：</label>
                            <div class="col-sm-10">
                                <!--{foreach from=$city_array item=city}-->
                                <div class="fl" style="margin: 0px 10px 10px 0px;">
                                    <input type="checkbox" class="square-radio" id="city-checkbox-<!--{$city.id}-->" name="cityID" value="<!--{$city.id}-->">
                                    <label for="city-checkbox-<!--{$city.id}-->"><!--{$city.name}--></label>
                                </div>
                                <!--{/foreach}-->
                            </div>
                        </div>

                        <div>
                            <!--{assign var="loop" value="4"}-->
                            <!--{assign var="formFlg" value="0"}-->
                            <!--{section name="loop" loop=$loop}-->
                                <!--{assign var="formFlg" value=$formFlg+1}-->
                                <div id="form_<!--{$formFlg}-->">
                                    <!--{if $formFlg == 1}-->
                                        <div class="form-group">
                                            <label for="concessions_<!--{$formFlg}-->" class="col-sm-2 control-label"><span class="red">*</span> 立减金额：</label>
                                            <div class="col-sm-10">
                                                <input type="text" class="form-control w300" id="concessions_<!--{$formFlg}-->" name="concessions_<!--{$formFlg}-->" placeholder="请输入立减金额【必填项】" >
                                            </div>
                                        </div>

                                        <div class="form-group">
                                            <label for="selectorID_<!--{$formFlg}-->" class="col-sm-2 control-label" data-toggle="dropdown"> 商品规则：</label>
                                            <div class="col-sm-10">

                                                <select class="form-control w150 fl" name="selectorID_<!--{$formFlg}-->" id="selectorID_<!--{$formFlg}-->" >
                                                    <option value="" selected>请选择</option>
                                                    <!--{foreach from=$promotionBillInfo->goodsSelector item=s_l}-->
                                                    <option value="<!--{$s_l->selectID}-->"><!--{$s_l->selectName}--></option>
                                                    <!--{/foreach}-->
                                                </select>
                                            </div>
                                        </div>

                                    <!--{elseif $formFlg == 2 }-->
                                        <div class="form-group">
                                            <label for="selectorID_<!--{$formFlg}-->" class="col-sm-2  control-label" data-toggle="dropdown" > 商品规则：</label>
                                            <div class="col-sm-10">

                                                <select class="form-control w150 fl" name="selectorID_<!--{$formFlg}-->" id="selectorID_<!--{$formFlg}-->" >
                                                    <option value="" selected>请选择</option>
                                                    <!--{foreach from=$promotionBillInfo->goodsSelector item=s_l}-->
                                                    <option value="<!--{$s_l->selectID}-->"><!--{$s_l->selectName}--></option>
                                                    <!--{/foreach}-->
                                                </select>
                                            </div>
                                        </div>
                                    <!--{elseif $formFlg == 3}-->

                                        <div class="form-group">
                                            <label for="selectorID_<!--{$formFlg}-->" class="col-sm-2 control-label" data-toggle="dropdown" > 商品规则：</label>
                                            <div class="col-sm-10">

                                                <select class="form-control w150 fl" name="selectorID_<!--{$formFlg}-->" id="selectorID_<!--{$formFlg}-->" >
                                                    <option value="" selected>请选择</option>
                                                    <!--{foreach from=$promotionBillInfo->goodsSelector item=s_l}-->
                                                    <option value="<!--{$s_l->selectID}-->"><!--{$s_l->selectName}--></option>
                                                    <!--{/foreach}-->
                                                </select>
                                            </div>
                                        </div>

                                        <input type="text" name="bill_status" value="4" id="bill_status" style="display: none;">
                                        <div class="form-group">
                                            <label for="bill_status" class="col-sm-2 control-label"> 发放订单状态：</label>
                                            <div class="col-sm-10">
                                                <select class="form-control w150 fl" name="bill_status" id="bill_status" >
                                                    <option value="0">订单建立</option>
                                                    <option value="4">订单完成</option>
                                                </select>
                                            </div>
                                        </div>

                                        <div class="form-group">
                                            <div>
                                                <label class="col-sm-2 control-label"><span class="red">*</span> 优惠券：</label>
                                                <div class="col-sm-10">
                                                    <table id="couponTable">
                                                        <tr id="addCouponTr_1">
                                                            <td class="td_input" style="height:35px;">
                                                                <div class="col-sm-3" style="margin:0 -15px" >
                                                                   <input type="text" class="form-control" id="couponNum_1" name="couponNum_1" maxlength="7" autocomplete="off"  style="ime-mode:active;" >
                                                                </div>
                                                                <div class="fl">
                                                                    <label class="col-sm-1 control-label">张</label>
                                                                </div>
                                                                 <select class="form-control fl" style="width: 250px;" name="couponCode_1" id="couponCode_1" >
                                                                 <option value="" selected>请选择</option>
                                                                 <!--{foreach from=$promotionBillInfo->coupon item=c_l}-->
                                                                 <option value="<!--{$c_l->couponID}-->"><!--{$c_l->couponName}--></option>
                                                                 <!--{/foreach}-->
                                                                </select>
                                                                <input type="button" style="margin:0 20px;"  onclick="promotion.coupoonList();" class="btn btn-success" value="添加">
                                                            </td>
                                                        </tr>
                                                    </table>
                                                </div>
                                            </div>
                                        </div>


                                    <!--{elseif $formFlg == 4}-->

                                    <div class="form-group">
                                        <label for="concessions_<!--{$formFlg}-->" class="col-sm-2 control-label"><span class="red">*</span> 折扣：</label>
                                        <div class="col-sm-10">
                                            <input type="text" class="form-control w300" id="concessions_<!--{$formFlg}-->" name="concessions_<!--{$formFlg}-->" placeholder="请输入折扣数【必填项】" >
                                        </div>
                                    </div>

                                    <div class="form-group">
                                        <label for="selectorID_<!--{$formFlg}-->" class="col-sm-2 control-label" data-toggle="dropdown"> 商品规则：</label>
                                        <div class="col-sm-10">

                                            <select class="form-control w150 fl" name="selectorID_<!--{$formFlg}-->" id="selectorID_<!--{$formFlg}-->" >
                                                <option value="" selected>请选择</option>
                                                <!--{foreach from=$promotionBillInfo->goodsSelector item=s_l}-->
                                                <option value="<!--{$s_l->selectID}-->"><!--{$s_l->selectName}--></option>
                                                <!--{/foreach}-->
                                            </select>
                                        </div>
                                    </div>


                                    <!--{/if}-->
                                </div>
                            <!--{/section}-->
                        </div>
                    </form>
                </div>
					
			</div>
		</div>

		<div class="popup-footer">
	        <div class="rectbox tac" style="margin: 20px;">
	        	<input type="hidden" id ="dataCache" name="dataCache" value="">
	            <input type="button" class="btn btn-success" onclick="promotion.check();" value="保存" />&nbsp;&nbsp;&nbsp;
	            <input type="button" class="btn btn-default" onclick="E.closeWindows();" value="关闭" />
	        </div>
	    </div>

	</div>
	<div id="send_cover" class="e_cover_layer hide" style="z-index:499;"></div>
	<div id="send_layer" class="e_layer hide" style="z-index:500;">
		<table style="border-collapse: collapse;margin: 0 auto;width: 300px;">
			<tr>
				<td>
					<div class="content" style="top:-15px;"></div>
				</td>
			</tr>
		</table>
	</div>
	<script>

          $(window).ready(function() {

              $('.square-radio').iCheck({
                  checkboxClass: 'icheckbox_square-blue',
                  radioClass: 'iradio_square-blue',
                  increaseArea: '20%' // optional
              });

              $('input[name="use_card"]').on('ifChecked', function(event){
                  var use_card = $(this).val();


                  if ( use_card == 0 ) {
                      $("#setUseCard").hide();

                  } else if( use_card ==1 ) {
                      $("#setUseCard").show();
                  } else {
                      $("#setUseCard").hide();
                  }

              });

              $('input[name="payTypeID"]').on('ifChanged', function() {
                  promotion.getPaymen();
              });

          });


		var GTYPE = 1 ;	
		var COUPONFLG = 100;	//添加优惠券行标志	
		var promotion = {
			//添加优惠券输入行
			coupoonList : function(){
				select_str = $('#couponCode_1').html();
				
				//console.log(select_str);
				var tr_str = '<tr id=addCouponTr_'+COUPONFLG+' style="height:35px;">';
						tr_str += '<td class="td_input">';
                            tr_str += '<div class="col-sm-3" style="margin:0 -15px" >';
                            tr_str += '<input id=couponNum_'+COUPONFLG+' type="text" class="form-control" name=couponNum_'+COUPONFLG+' maxlength="7" autocomplete="off"  style="ime-mode:active;" >';
                            tr_str += '</div>';
                            tr_str += '<div class="fl">';
                            tr_str += '<label class="col-sm-1 control-label">张</label>';
                            tr_str += '</div>';
                            tr_str += '<select name=couponCode_'+COUPONFLG+' id=couponCode_'+COUPONFLG+' class="form-control fl" style="width: 250px;" >';
								tr_str += select_str;
							tr_str += '</select>';	
							tr_str += ' <input type="button" class="btn btn-default" style="margin:0 20px;" onclick=promotion.delCoupon('+COUPONFLG+') value="移除">';
						tr_str += '</td>';
					tr_str += '</tr>';
					
					COUPONFLG++
					$('#couponTable').append(tr_str);
			},
			
			//删除添加优惠券的行
			delCoupon : function(flg){
				$('#addCouponTr_'+flg).remove();
			},
			
			//切换层
			change: function(index){
				var selectStr = $.trim($('#select_'+index).html());
				$('#title').html(selectStr);
				$('#select_'+index).siblings().removeClass();
				$('#select_'+index).addClass('active');
				$('#form_'+index).siblings().css('display','none');
				$('#form_'+index).css('display','block');
				$("#type").val(index) ;

						
				GTYPE = index ;
			},
			//自动添加一行数据
			addLine: function ( goodsLen ){
				var sku		  = $.trim($("#sku").attr("title")) ; 		//商品编码
				var postID	  = $.trim($("#sku").attr("name")) ; 		//商品postID
				var goodsName = $.trim($("#goodsName").html()) ; 		//商品名称
				var spec	  = $.trim($("#spec").html()) ; 			//商品规格
				var salePrice = $.trim($("#salePrice").attr("title")) ; //商品销售价
				var saleAmount= $.trim($("#saleAmount").val()) ; 		//商品数量
				var rowClass  = goodsLen%2 == 0 ? '' : 'erow' ;
				var htmlLine =  '<tr id="row_' + postID + '" class="' + rowClass + '" onclick="doDelect('+ postID +')">' ; 
					htmlLine += '	<td class="">' ;
					htmlLine += '		<div style="width: 90px;height:30px;" class="sundefined">' ;
					htmlLine += 			sku ;
					htmlLine += '		</div>' ;
					htmlLine += '	</td>' ;
					htmlLine += '	<td class="">' ;
					htmlLine += '		<div style="width: 260px;" class="sundefined">' ;
					htmlLine += 			goodsName ;
					htmlLine += '		</div>' ;
					htmlLine += '	</td>' ;
					htmlLine += '	<td class="">' ;
					htmlLine += '		<div style="width: 120px;" class="sundefined">' ;
					htmlLine += 			spec ;
					htmlLine += '		</div>' ;
					htmlLine += '	</td>' ;
					htmlLine += '	<td class="">' ;
					htmlLine += '		<div style="width: 80px;" class="sundefined">' ;
					htmlLine +=				salePrice ;
					htmlLine += '		</div>' ;
					htmlLine += '	</td>' ;
					htmlLine += '	<td>' ;
					htmlLine += '		<div style="width: 50px;">' ;
					htmlLine += 			saleAmount ;		
					htmlLine += '		</div>';
					htmlLine += '	</td>' ;
					htmlLine += '</tr>' ; 

				$("#goodsTable").append(htmlLine) ;
				
			},
			
			//ajax 回调函数
	        getGoodsInfoCallBack : function (response){
	            if(response.code != 200){
	                $("#sku").val("") ;
	                E.alert(response.message);
	                $("#sku").focus() ;
	                return false;
	            }
	            $("#sku").attr("name" , response.data.postID) ;
				$("#sku").attr("title" , response.data.sku) ;
				$("#goodsName").html( response.data.goodsName ) ;
				$("#spec").html( response.data.spec ) ;
				$("#salePrice").val(0) ;
				$("#saleAmount").val(1) ;
				$("#salePrice").focus() ;
	        },
	        
	        //清除行里的数据
	        clear: function () {
	        	$("#sku").val("") ;
	        	$("#sku").attr("title", "") ;
	        	$("#sku").attr("name", "") ;
	        	$("#goodsName").html("") ;
	        	$("#spec").html("") ;
	        	$("#salePrice").html("") ;
	        	$("#saleAmount").val("") ;
	        },
	        removeRow:function () {
	        	//从大数组中删除选择的商品
	            $(".trSelected").each(function (k,v){
	                var id = $(this).attr("id");
	              	var idArr = id.split("_") ;
	              	postID = idArr[1] ;
	              	if( postID !=0 ) {
	              		//移出这一行
	              		$("#row_" + postID).remove() ;
	              		
	              		//读取data
	              		var goods_obj = $("#dataCache").data("goods")
						if(goods_obj){
							$.each(goods_obj,function(k,v){
								if( postID == k )
									delete(goods_obj[k])
							});
						}
						//保存商品到data
						$("#dataCache").data( "goods" , goods_obj )
						
	              	}
	              	
	            });
	        }
	        ,
			//检查数据
	        check: function() {
				this.dt = E.getFormValues("sendForm") ;
				
	            var concessions = $.trim($("#concessions_" + GTYPE).val()) ;

	            var error_msg = "";
	            if (this.dt.title == "")
	                error_msg += "促销标题不能为空<br />";
	                
	            if (!E.isDate(this.dt.startTime))
	                error_msg += "促销开始时间格式不对<br />";
	                
				if (!E.isDate(this.dt.endTime))
					error_msg += "促销结束时间格式不对<br />";
	                
	             if(this.dt.startTime > this.dt.endTime)   
	             	error_msg += "促销开始时间不能大于结束时间<br />";

                if(this.dt.sendStartTime > this.dt.sendEndTime)
                    error_msg += "配送开始时间不能大于结束时间<br />";
	                
				/*if (!E.isInt(this.dt.priority))
					error_msg += "优先级必须为数字<br />";*/
					
				 if (this.dt.billAmount == "" ||  !E.isMoney(this.dt.billAmount))
	                error_msg += "输入的下限金额格式不对<br />";


                if (parseFloat(concessions) > parseFloat(this.dt.billAmount) ){
                    error_msg += "立减金额不可以大于下限金额<br/>";
                }

                if(this.dt.use_card == 1 && $.isEmptyObject(this.dt.use_card_face_value)){
                    error_msg += '请选择代金卡面值<br/>';
                }

                if( GTYPE == 1 ){	//订单立减

	            	if(!E.isMoney(concessions))
	            		error_msg += "输入的立减金额格式不对<br />";

	            }else if( GTYPE == 3 ){	//优惠券
	            	//优惠券张数
	            	$.each($('[name*=couponNum_]'), function(k, v){
	            		var couponNum = $.trim($(v).val());
	            		if(!E.isInt(couponNum) || couponNum == 0){
	            			error_msg += "优惠券的张数必须为正整数<br />";
	            			return false;
	            		}
	            	})

	            	//优惠券编号
	            	var couponType = [];
	            	$.each($('[name*=couponCode_]'), function(k, v){
	            		var couponCode = $.trim($(v).val());
	            		if(!E.isInt(couponCode) || couponCode == 0){
	            			error_msg += "优惠券的编号必须为正整数<br />";
	            			return false;
	            		}
	            		couponType.push(couponCode);
	            	})

	            	//判断数组中是否存在同一元素
	            	var repeat = promotion.checkRepeat(couponType);
	            	if(repeat)
	            		error_msg += "优惠券类型不能重复<br />";

	            	//添加优惠券行数参数
	            	var couponObj = [];
	            	$.each($('[name*=couponNum_]'), function(k, v){
	            		var numID = $(v).attr('id');
	            		var numArray = numID.split('_');

	            		//获取优惠券张数和优惠券编号
	            		var couponNum = $('#couponNum_'+numArray[1]).val();
	            		var couponID  = $('#couponCode_'+numArray[1]).val();
	            		couponObj.push([couponNum, couponID]);
	            	})
	            	this.dt['couponLine'] = couponObj;

	            }else if( GTYPE == 4){
                    if(!E.isMoney(concessions) || concessions < 0 || concessions >=1){
                        error_msg += "折扣不能为空且必须为0到1之间的小数<br />";
                    }
                }

			    
	            if (error_msg != "")
	                E.alert(error_msg);
	            else
	                promotion.add() ;
	        },
				
			//保存促销信息
	        add: function() {
	            $.ajax({
	                type: "POST",
	                url: "/invokingAjax-backend/promotion/promotionBill.ajax?operFlg=100&act=edit" ,
	                dataType: "JSON",
	                data: this.dt,
	                success: function( result_obj ) {
	                    if (result_obj.code == 200) {
                        	E.opener("promotion.load");
                        	E.alert(result_obj.message, 2);
                        	self.location = "/quanliadmin/app/promotion/promotionBill.pscript?operFlg=100" ;
	                    } else {
	                        E.alert(result_obj.message);
	                    }
	                }
	            });
	        },

            //查询支付方式下的付款方式
            getPaymen:function( payTypeID ){

                var dt = E.getFormValues("sendForm");

                E.loadding.open('正在努力加载中...');

                E.ajax_post({
                    url: '/invokingAjax-backend/promotion/promotionBill.ajax?operFlg=7',
                    data: dt,
                    call: 'promotion.getPaymenCallBack'
                });
            },

            getPaymenCallBack:function( result_data ){

                E.loadding.close();
                if( result_data.code == 200 ){

                    if(result_data.data){
                        var html = '<label class="col-sm-2 control-label">付款方式：</label>';
                        html += '<div class="col-sm-10">'
                        $.each(result_data.data,function(k,v){
                            var checked = '';
                            if(v.select == 1){
                                checked = 'checked';
                            }
                            html += '<div class="fl" style="margin: 0px 10px 10px 0px;">';
                            html += '<input type="checkbox" class="square-radio-payment" id="payment-checkbox-'+ v.paymentID +'" name="paymentID" value="'+ v.payTypeID + '-' + v.paymentID +'" '+ checked +'>';
                            html += '<label for="payment-checkbox-'+ v.paymentID +'">'+ v.name +'</label>';
                            html += '</div>';

                        });
                        html += '</div>';

                        $("#payment").html(html).show()

                        $('.square-radio-payment').iCheck({
                            checkboxClass: 'icheckbox_square-blue',
                            radioClass: 'iradio_square-blue',
                            increaseArea: '20%' // optional
                        });

                    }else{
                        $("#payment").empty().hide();
                    }

                }else{

                    E.alert(result_data.message);

                }

            },
	        
	        //检查数组中是否有重复的元素
	        checkRepeat : function(a){
	        	return /(\x0f[^\x0f]+)\x0f[\s\S]*\1/.test("\x0f"+ a.join("\x0f\x0f") +"\x0f");
	        }
		};
		
		promotion.change(1) ;
		
		$(document).ready(function(){
			//商品输入sku 回车
		    $("#sku").keydown(function(e){
		        if(e.keyCode == 13){
		        	var goodsSku = $.trim($("#sku").val()) ;
		        	if(goodsSku == "") {
		        		 E.alert('请输入商品SKU');
		        		 return false ;
		        	}
		        	//检查商品编码是否已经存在
		        	var goods_obj = $("#dataCache").data("goods")
					if(goods_obj){
						var flg = 0
						$.each(goods_obj,function(k,v){
							if(goodsSku == v[0])
								flg = 1;
						});
						
						if(flg == 1){
							E.alert("已经存在SKU「" +  goodsSku + "」的商品");
							$("#sku").focus() ;
							return false;
						}
					}
		        	
					promotion_ajax( 6, {sku : goodsSku } , promotion.getGoodsInfoCallBack ) ;
		        }
		    });
			
		    //销售价格回车
		    $("#salePrice").keydown(function(e){
		        if(e.keyCode == 13){
		        	var salePrice = $.trim($("#salePrice").val()) ;
		        	if(goodsName == "")
		        		return false ;	
		        	if(salePrice == "" || !E.isMoney(salePrice)) {
		        		 E.alert('请输入正确的商品价格');
		        		 return false ;
		        	}
		        	$("#salePrice").attr("title" ,salePrice) ;
					$("#saleAmount").focus() ;
		        }
		    });
		    
		     //销售价格失去焦点
		    $("#salePrice").blur(function(e){
	        	var salePrice = $.trim($("#salePrice").val()) ;
	        	if(goodsName == "")
	        		return false ;	
	        	if(salePrice == "" || !E.isMoney(salePrice)) {
	        		 E.alert('请输入正确的商品价格');
	        		 return false ;
	        	}
	        	$("#salePrice").attr("title" ,salePrice) ;
				$("#saleAmount").focus() ;
		        
		    });
		    
		    //商品数量回车
		    $("#saleAmount").keydown(function(e){
		        if(e.keyCode == 13){
		        	var saleAmount = $.trim($("#saleAmount").val()) ;			//商品数量
		        	var postID 	   = $("#sku").attr("name") ;					//商品网购号
		        	var goodsSku   = $.trim($("#sku").attr("title")) ;			//商品sku
		        	var goodsName  = $.trim($("#goodsName").html()) ;			//商品名称
		        	var salePrice  = $.trim($("#salePrice").attr("title")) ;	//实售价格
		        	
		        	if(goodsName == "")
		        		return false ;
		        		
		        	if(saleAmount == "" || !E.isInt(saleAmount)) {
		        		 E.alert('请输入正确的商品数量');
		        		 return false ;
		        	}
		        	
		        	var goods_obj = $("#dataCache").data("goods");
					if(!goods_obj)
						goods_obj = {};
					
					goods_obj[postID] =[goodsSku ,postID ,saleAmount,salePrice];
					$("#dataCache").data("goods",goods_obj);
					var goodsLen = $("#goodsTable tr").length
					promotion.addLine( goodsLen ) ;
					promotion.clear() ;
					$("#sku").focus() ;
					
		        }
		    });
		    
		    //当数量失去焦点
		    $("#saleAmount").blur(function(e){
		    	var saleAmount = $.trim($("#saleAmount").val()) ;	
	        	var goodsName  = $.trim($("#goodsName").html()) ;
		      	if(goodsName != ""){
		      		if(saleAmount == "" || !E.isInt(saleAmount)) {
		        		 E.alert("请输入正确的商品数量");
		        		 $("#saleAmount").focus() ;
		        		 return false ;
		        	}
		      	}
		    });
		    
		})
		
		//选中要删除的行
		function doDelect ( postID ){
			var className = $("#row_" + postID).attr("class") ;
			if(className == "")
				$("#row_" + postID).addClass("trSelected") ;
			else
				$("#row_" + postID).removeClass("trSelected") ;
			if(className == "erow")
				$("#row_" + postID).addClass("trSelected erow") ;
	
			if(className == "trSelected erow")
				$("#row_" + postID).removeClass("trSelected") ;
		}
		
		//查询商品ajax 
	    function promotion_ajax( flg , data , func   ){
	        $.ajax({
	            type: "post",
	            url: "/invokingAjax-backend/promotion/promotionBill.ajax?operFlg=" + flg ,
	            dataType: "json",
	            data: data,
	            success: function( result_obj ) {
	                func(result_obj);
	            },
	            error : function(msg){
	                E.alert('网络不给力，请重新尝试...',1);
	            }
	        });
    	}
	</script>
	<script>
        $('#startTime').datetimepicker({timeFormat:'HH:mm:ss',showHour: true,showMinute: true,showSecond: true,});
        $('#endTime').datetimepicker({timeFormat:'HH:mm:ss',showHour: true,showMinute: true,showSecond: true,hour: 23,minute: 59,second:59,});

        $('#sendStartTime').datetimepicker();
        $('#sendEndTime').datetimepicker();
	</script>
</body>
</html>